package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一串罗马数字:");
        String romanNum = sc.next();
        System.out.println(romanNum + "转换成为阿拉伯数字后是" + romanToInt(romanNum));

    }

    public static int romanToInt(String s) {

        int[] transform = new int[127];
        transform['I'] = 1;
        transform['V'] = 5;
        transform['X'] = 10;
        transform['L'] = 50;
        transform['C'] = 100;
        transform['D'] = 500;
        transform['M'] = 1000;

        int ans = transform[s.charAt(0)];

        for(int i = 0; i < s.length() - 1; i++){
            if(transform[s.charAt(i)] >= transform[s.charAt(i+1)]){
                ans += transform[s.charAt(i+1)];
            }else{
                ans += transform[s.charAt(i+1)] - 2 * transform[s.charAt(i)];
            }
        }
        return ans;
    }
}
